package cRpc

import (
	"github.com/gin-gonic/gin"
)

type ClientType string
type ParseHostFunc func(ctx *gin.Context, app, service string) (host, uri string)
type SignatureEncryptFunc func(ctx *gin.Context, app, service string) string
type SignatureDecryptFunc func(ctx *gin.Context, app, service string) bool

const (
	RpcConfigName string = "RpcConf"

	GRPCClient ClientType = "grpc"
	HTTPClient ClientType = "http"

	HTTPRequestUri string = "/rpc/call"
	HTTPTimeout    int64  = 60
	HTTPTryTimes   int64  = 3
)

type RpcConf struct {
	RequestURI      string               `json:"request_uri"`
	Timeout         int64                `json:"timeout"`
	Try             int64                `json:"try"`
	Signature       string               `json:"signature"`
	Clients         []RpcConf_Client     `json:"clients"`
	HostFunc        ParseHostFunc        `json:"-"`
	SignEncryptFunc SignatureEncryptFunc `json:"-"`
	SignDecryptFunc SignatureDecryptFunc `json:"-"`
}

type RpcConf_Client struct {
	Type     ClientType               `json:"type"`
	Host     string                   `json:"host"`
	Package  string                   `json:"package"`
	Services []RpcConf_Client_Service `json:"services"`
}

type RpcConf_Client_Service struct {
	App     string `json:"app"`
	Service string `json:"service"`
}

func (i *RpcConf) ConfigName() string {
	return RpcConfigName
}

func (i *RpcConf) Init() {
}

func (i *RpcConf) SetHostFunc(f ParseHostFunc) {
	i.HostFunc = f
}

func (i *RpcConf) SetSignEncryptFunc(f SignatureEncryptFunc) {
	i.SignEncryptFunc = f
}

func (i *RpcConf) SetSignDecryptFunc(f SignatureDecryptFunc) {
	i.SignDecryptFunc = f
}

var rpc_config = &RpcConf{}
